#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
#include <exception/NpException.h>
#include <gather/Gather.h>
#include <config/NovelConfig.h>
#include <utils/XTimer.h>
#include "AnalyseChapter.h"

using namespace std;
using namespace ruleconfiguration;

AnalyseChapter::AnalyseChapter()
{
  m_contentTxt.reserve(SIZE_CONTENT_TXT);
}

void AnalyseChapter::analyseChapterInfo(chapterInfo::ChapterInfo& chapter)
{
  if(!checkContentIsImage())
  {
    analyseTxt();
  }
  else
  {
    analyseImage(chapter);
  }
}

bool AnalyseChapter::checkContentIsImage()
{
  static string txtLink;
  matchSingleValue("PubContentImages", txtLink);
  return !txtLink.empty();
}

void AnalyseChapter::analyseTxt()
{
  static string txtLink;
  matchSingleValue("PubTextUrl", txtLink);
  if(!txtLink.empty())//如果为JS调用TXT，需要先将TXT下载
  {
    m_contentTxt =  Gather::getInstance()->collect(txtLink.c_str());//txt获取失败
  }
  else
  {
    matchSingleValue("PubContentText", m_contentTxt);
  }
  replaceMultiMatchValue("PubContentReplace", m_contentTxt);
}

void AnalyseChapter::analyseImage(chapterInfo::ChapterInfo& chapter)
{
  RegexInfo* regImage = m_rule.getRegex("PubContentImages");
  boost::regex regexImage(regImage->Pattern);
  std::string::const_iterator itr_begin = m_content.begin();
  std::string::const_iterator itr_end = m_content.end();
  boost::smatch matchs;
  if(boost::regex_search(itr_begin, itr_end, matchs, regexImage, regImage->Options))
  {
    attachInfo::AttachInfo attach;
    attach.imagelink = matchs[1];
    attach.isactive= "1";
    attach.articleid = m_article.articleid;
    attach.attachid = boost::lexical_cast<string>(NovelConfig::getInstance()->getMaxAttachID());
    attach.chapterid = chapter.chapterid;
    attach.uptime = boost::lexical_cast<string>(XTimer::getDateTime());
    m_attachList.push_back(attach);
    itr_begin = matchs[0].second;
  }
}

attachInfo::AttachInfo& AnalyseChapter::operator [](unsigned int index)
{
  throwExIf(index >= m_attachList.size(), npexception:: INVALID_LIST_INDEX);
  return m_attachList[index];
}

void AnalyseChapter::replaceMultiMatchValue(const std::string& regexName, std::string& content)
{
  typedef std::string::const_iterator StringIterator;
  ruleconfiguration::RegexInfo* reg = m_rule.getRegex(regexName);

  unsigned int posBegin = 0;
  unsigned int pos = 0;
  string filter;

  if(reg->Pattern.length() >0)
  {
    posBegin = 0;
    pos = 0;
    while((pos = reg->Pattern.find("$", posBegin)) != string::npos)
    {
      filter  = reg->Pattern.substr(posBegin,   pos - posBegin);
      boost::regex regexExpress(filter);
      content = boost::regex_replace(content, regexExpress, "", reg->Options);
      posBegin = pos +1;
    }
    filter = reg->Pattern.substr(posBegin,  reg->Pattern.length() -  posBegin);
    boost::regex regexExpressLast(filter);
    content = boost::regex_replace(content, regexExpressLast, "", reg->Options);
  }
  if(reg->FilterPattern.length()>0)
  {
    while((pos = reg->FilterPattern.find("$", posBegin)) != string::npos)
    {
      filter = reg->FilterPattern.substr(posBegin,   pos - posBegin);
      XString::replaceAll(content, filter, "");
      posBegin = pos +1;
    }
    filter = reg->FilterPattern.substr(posBegin,  reg->FilterPattern.length() -  posBegin);
    XString::replaceAll(content, filter, "");
  }
}
